
import { defineStore } from "pinia";
import{ref} from "vue";

export const useUserStore = defineStore("user",
  ()=>{
   
   let token=ref(sessionStorage.getItem('token') || null);//要是响应式
   let userId=ref(sessionStorage.getItem('userId') || null);
   let companyId=ref(sessionStorage.getItem('companyId') || null);
  // 定义 mutations
  
  const   setToken=  (newToken) => {
    
      token.value = newToken;
    
      //缓存存储token
      sessionStorage.setItem("token", newToken);
    }
    const removeToken=  () =>{
      token.value = null;
      //删除缓存存储的token
      sessionStorage.removeItem("token");
    };
    const   setUserId=  (userId)=>  {
        const newUserId = userId;
        sessionStorage.setItem("userId", newUserId);
    }
    const   setCompanyId=  (companyId)=>  {
      sessionStorage.setItem("companyId", companyId);
  }
  
return {
  companyId,
  userId,
  token,
  setToken,
  removeToken,
  setUserId,
  setCompanyId

}
  }
  

   
  

   
  
);
